rm(list = ls())

## Tidy up the data first
library(survey)
library(foreign)
library(doBy)
library(xtable)

cces <- read.dta("CCES_2012b.dta")

primary_type_dems <- read.csv("Primary Dates and Types 2008-2014 - Dems.csv",colClasses="character")
primary_type_reps <- read.csv("Primary Dates and Types 2008-2014 - Reps.csv",colClasses="character")

# recode dates as days from January 1
the_dates_dems <- as.Date(strptime(primary_type_dems$X2012.President.Date,format="%m/%d/%y"))
primary_type_dems$dem_days <- as.numeric(the_dates_dems - as.Date("2012-01-01"))

the_dates_reps <- as.Date(strptime(primary_type_reps$X2012.President.Date,format="%m/%d/%y"))
primary_type_reps$rep_days <- as.numeric(the_dates_reps - as.Date("2012-01-01"))

cces$presgeneral_party<-NA
cces$presgeneral_party[cces$president_post_vote==1] <-"Democrat"
cces$presgeneral_party[cces$president_post_vote==0] <-"Republican"


# jitter days
primary_type<-as.data.frame(cbind(primary_type_dems$abb, primary_type_dems$dem_days, primary_type_reps$rep_days, primary_type_dems$X2012.President.Type, primary_type_reps$X2012.President.Type, primary_type_dems$X2012.Same.Date,primary_type_reps$X2012.Same.Date))

colnames(primary_type)<-c("abb", "dem_days", "rep_days","dem_type", "rep_type", "dems_pres_cong_sameday", "reps_pres_cong_sameday")
primary_type$abb<-as.vector(primary_type$abb)
primary_type$dem_days<-as.numeric(as.vector(primary_type$dem_days))
primary_type$rep_days<-as.numeric(as.vector(primary_type$rep_days))

# merge the data
data <- cbind(primary_type[match(cces$abb,primary_type$abb),],
              cces)

data$voted_primary_validated<-NA
data$voted_primary_validated[data$voted_primary_pres_validated!="NA"]<-1
table(data$voted_primary_validated)
data$voted_primary_validated[data$dems_pres_cong_sameday==1 & data$pid3==1 & data$voted_primary_cong_validated!="NA"]<-1
table(data$voted_primary_validated)
data$voted_primary_validated[data$reps_pres_cong_sameday==1 & data$pid3==3 & data$voted_primary_cong_validated!="NA"]<-1
table(data$voted_primary_validated)


# order the data by number of days from Jan 1
data <- data[order(data$dem_days),]

# normalize ideology estimates
data$est <- (data$est - mean(data$est,na.rm=T))/sd(data$est)

# scap4 = retrospective primary choice (september)
# bcap4 = prospective primary choice (baseline)
# different primary dates...
# p = post, j = january, m = march, o=october
# e2012p = validated primaryturnout & registration
data$primaryturnout <- NA
# first, use known primary primaryturnout and self reports of *which* primary
data$primaryturnout[data$president_post_vote=="0" & data$voted_primary_validated == "1" ] <- "Republican"
data$primaryturnout[data$president_post_vote=="1" & data$voted_primary_validated == "1" ] <- "Democrat"

# for caucuses, make the primaryturnout variable NA since we don't know anything about their primaryturnout
data$primaryturnout[data$rep_type=="Caucus"] <- "NA"
data$primaryturnout[data$dem_type=="Caucus"] <- "NA"

cces12<-data

cces12$pid7<-as.numeric(as.vector(cces12$pid7))


cces12$voted_primary_validated[is.na(cces12$voted_primary_validated) & !is.na(cces12$president_post_vote)]<-0

cces12$general_noprimary[cces12$voted_general_validated==1 & cces12$voted_primary_validated==0]<-1
cces12$general_noprimary[is.na(cces12$general_noprimary) & !is.na(cces12$president_post_vote)]<-0

cces12$gender<-as.numeric(as.vector(cces12$gender))
cces12$idealpoint<-as.numeric(as.vector(cces12$est))
cces12$self_ideology<-as.numeric(as.vector(cces12$self_ideology))
cces12$donor<-as.numeric(as.vector(cces12$donor))
cces12$political_interest2<- as.numeric(as.vector(cces12$political_interest2))
cces12$hs[cces12$education==2]<-1
cces12$hs[cces12$education!=2]<-0

cces12$pol_interest[cces12$political_interest2==1]<-1
cces12$pol_interest[cces12$political_interest2!=1]<-0

cces12$college[cces12$education>=4]<-1
cces12$college[cces12$education<4]<-0

cces12$white[cces12$race==1]<-1
cces12$white[cces12$race!=1]<-0

cces12<-subset(cces12, !is.na(weight) )


cces12$weight<-as.numeric(as.vector(cces12$weight))
cces12.design<-svydesign(ids=cces12$respondent, data = cces12, weights=cces12$weight)

cces12_primary<-subset(cces12,primaryturnout!="0")
cces12_primary.design<-svydesign(ids=cces12_primary$respondent, data = cces12_primary, weights=cces12_primary$weight)

cces12_general<-subset(cces12,cces12$voted_general_validated==1 )
cces12_general.design<-svydesign(ids=cces12_general$respondent, data = cces12_general, weights=cces12_general$weight)


cces12_general_noprim<-subset(cces12,(voted_general_validated==1 & voted_primary_validated!=1))
cces12_general_noprim.design<-svydesign(ids=cces12_general_noprim$respondent, data = cces12_general_noprim, weights=cces12_general_noprim$weight)

## make density graph of ideal points
mean_idealpoint_primary_dems <- svymean(cces12_primary$idealpoint[cces12_primary$primaryturnout=="Democrat"], design=cces12_primary.design[cces12_primary$primaryturnout=="Democrat"], na.rm=T)

mean_idealpoint_partyfollowing_dems <- svymean(cces12_general$idealpoint[cces12_general$presgeneral_party=="Democrat" | cces12_general$pid7<4], design=cces12_general.design[cces12_general$presgeneral_party=="Democrat" | cces12_general$pid7<4], na.rm=T)

mean_idealpoint_primary_reps <- svymean(cces12_primary$idealpoint[cces12_primary$primaryturnout=="Republican"], design=cces12_primary.design[cces12_primary$primaryturnout=="Republican"], na.rm=T)

mean_idealpoint_partyfollowing_reps <- svymean(cces12_general$idealpoint[cces12_general$presgeneral_party=="Republican" | cces12_general$pid7>4], design=cces12_general.design[cces12_general$presgeneral_party=="Republican" | cces12_general$pid7>4], na.rm=T)

library(ggplot2)
cces12$Party<-cces12$pid3
cces12$Party[cces12$pid3==2]<-NA
cces12$Party[cces12$pid3=="NA"]<-NA
cces12$Party<-as.character(cces12$Party)
pdf('density_cces12_pres.pdf', height = 4, width = 7)
ggplot(subset(cces12, !is.na(Party)), aes(idealpoint, fill = Party)) +
geom_density(alpha = 0.1)+ scale_fill_manual(breaks = c(1, 3),
                        values=c("blue",  "red"),labels=c("Democrats", "Republicans"),guide = FALSE)+
    geom_vline(xintercept = mean_idealpoint_primary_dems[1], linetype="longdash")+
     geom_vline(xintercept = mean_idealpoint_partyfollowing_dems[1])+
    geom_vline(xintercept = mean_idealpoint_primary_reps[1], linetype="longdash")+
    geom_vline(xintercept = mean_idealpoint_partyfollowing_reps[1])+
      xlab("Ideal Point") +
      annotate("text",label="Democrats",x=-1.2,y=.3,size=4)+
       annotate("text",label="Republicans",x=1.4,y=.3,size=4)+
        theme_bw() 
dev.off()

## cross-tabs of who voted
# by party

cces12$voted_primary_validated<-as.numeric(as.vector(cces12$voted_primary_validated))
cces12$general_noprimary<-as.numeric(as.vector(cces12$general_noprimary))
cces12$voted_primary_validated<-as.numeric(as.vector(cces12$voted_primary_validated))
cces12$general_noprimary<-as.numeric(as.vector(cces12$general_noprimary))


svymean(cces12$voted_primary_validated[cces12$pid7<4], design=cces12.design[cces12$pid7<4], na.rm=T)
svymean(cces12$general_noprimary[cces12$pid7<4], design=cces12.design[cces12$pid7<4], na.rm=T)

svymean(cces12$voted_primary_validated[cces12$pid7>4], design=cces12.design[cces12$pid7>4], na.rm=T)
svymean(cces12$general_noprimary[cces12$pid7>4], design=cces12.design[cces12$pid7>4], na.rm=T)

fulltable <- NULL


## age
col1<-svyquantile(~cces12_primary$age_exact[cces12_primary$primaryturnout=="Democrat"], design=cces12_primary.design[cces12_primary$primaryturnout=="Democrat"], na.rm=T, quantile=.5)
col2<-svyquantile(~cces12_general_noprim$age_exact[cces12_general_noprim$presgeneral_party=="Democrat"], design=cces12_general_noprim.design[cces12_general_noprim$presgeneral_party=="Democrat"], na.rm=T, quantile=.5)
col3<-svyquantile(~cces12_general$age_exact[cces12_general$presgeneral_party=="Democrat" | cces12_general$pid7<4], design=cces12_general.design[cces12_general$presgeneral_party=="Democrat" | cces12_general$pid7<4], na.rm=T, quantile=.5)

col4<-svyquantile(~cces12_primary$age_exact[cces12_primary$primaryturnout=="Republican"], design=cces12_primary.design[cces12_primary$primaryturnout=="Republican"], na.rm=T, quantile=.5)
col5<-svyquantile(~cces12_general_noprim$age_exact[cces12_general_noprim$presgeneral_party=="Republican"], design=cces12_general_noprim.design[cces12_general_noprim$presgeneral_party=="Republican"], na.rm=T, quantile=.5)
col6<-svyquantile(~cces12_general$age_exact[cces12_general$presgeneral_party=="Republican" | cces12_general$pid7>4], design=cces12_general.design[cces12_general$presgeneral_party=="Republican" | cces12_general$pid7>4], na.rm=T, quantile=.5)
diff_a<-col1-col3
diff_b<-col4-col6
z_test_a = (col1-col3)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col3)$SE^2)
z_test_b= (col4-col6)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col6)$SE^2)
xtable(as.matrix(cbind(col4, col5, col6)))

col4.mean<-svymean(cces12_primary$age_exact[cces12_primary$primaryturnout=="Republican"], design=cces12_primary.design[cces12_primary$primaryturnout=="Republican"], na.rm=T)
col5.mean<-svymean(cces12_general_noprim$age_exact[cces12_general_noprim$presgeneral_party=="Republican"], design=cces12_general_noprim.design[cces12_general_noprim$presgeneral_party=="Republican"], na.rm=T)
col6.mean<-svymean(cces12_general$age_exact[cces12_general$presgeneral_party=="Republican" | cces12_general$pid7>4], design=cces12_general.design[cces12_general$presgeneral_party=="Republican" | cces12_general$pid7>4], na.rm=T)
xtable(as.matrix(cbind(col4.mean, col5.mean, col6.mean)))
xtable(as.matrix(cbind(
paste("[",round(as.data.frame(col4.mean)$mean-1.96*as.data.frame(col4.mean)$SE,1), ",",round(as.data.frame(col4.mean)$mean+1.96*as.data.frame(col4.mean)$SE,1),
"]", sep=""),
paste("[",round(as.data.frame(col5.mean)$mean-1.96*as.data.frame(col5.mean)$SE,1), ",",round(as.data.frame(col5.mean)$mean+1.96*as.data.frame(col5.mean)$SE,1),
"]", sep=""),
paste("[",round(as.data.frame(col6.mean)$mean-1.96*as.data.frame(col6.mean)$SE,1), ", ",round(as.data.frame(col6.mean)$mean+1.96*as.data.frame(col6.mean)$SE,1),
"]", sep=""))))

#xtable(as.matrix(cbind(col1, col3, diff_a,NA, col4, col6, diff_b)))
xtable(as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6))); mat <- as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6)); rownames(mat) <- "age"; fulltable <- rbind(fulltable,mat)

## hs degree
col1<-svymean(cces12_primary$hs[cces12_primary$primaryturnout=="Democrat"], design=cces12_primary.design[cces12_primary$primaryturnout=="Democrat"], na.rm=T)
col2<-svymean(cces12_general_noprim$hs[cces12_general_noprim$presgeneral_party=="Democrat"], design=cces12_general_noprim.design[cces12_general_noprim$presgeneral_party=="Democrat"], na.rm=T)
col3<-svymean(cces12_general$hs[cces12_general$presgeneral_party=="Democrat" | cces12_general$pid7<4], design=cces12_general.design[cces12_general$presgeneral_party=="Democrat" | cces12_general$pid7<4], na.rm=T)

col4<-svymean(cces12_primary$hs[cces12_primary$primaryturnout=="Republican"], design=cces12_primary.design[cces12_primary$primaryturnout=="Republican"], na.rm=T)
col5<-svymean(cces12_general_noprim$hs[cces12_general_noprim$presgeneral_party=="Republican"], design=cces12_general_noprim.design[cces12_general_noprim$presgeneral_party=="Republican"], na.rm=T)
col6<-svymean(cces12_general$hs[cces12_general$presgeneral_party=="Republican" | cces12_general$pid7>4], design=cces12_general.design[cces12_general$presgeneral_party=="Republican" | cces12_general$pid7>4], na.rm=T)
diff_a<-col1-col3
diff_b<-col4-col6
z_test_a = (col1-col3)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col3)$SE^2)
z_test_b= (col4-col6)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col6)$SE^2)


col1<-paste(round(col1*100,0), "%",sep="")
col2<-paste(round(col2*100,0), "%",sep="")
col3<-paste(round(col3*100,0), "%",sep="")
col4<-paste(round(col4*100,0), "%",sep="")
col5<-paste(round(col5*100,0), "%",sep="")
col6<-paste(round(col6*100,0), "%",sep="")
diff_a<-paste(round(diff_a*100,0), "%",sep="")
diff_b<-paste(round(diff_b*100,0), "%",sep="")

#xtable(as.matrix(cbind(col1, col3, diff_a,NA, col4, col6, diff_b)))
xtable(as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6))); mat <- as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6)); rownames(mat) <- "hs degree"; fulltable <- rbind(fulltable,mat)

## college degree
col1<-svymean(cces12_primary$college[cces12_primary$primaryturnout=="Democrat"], design=cces12_primary.design[cces12_primary$primaryturnout=="Democrat"], na.rm=T)
col2<-svymean(cces12_general_noprim$college[cces12_general_noprim$presgeneral_party=="Democrat"], design=cces12_general_noprim.design[cces12_general_noprim$presgeneral_party=="Democrat"], na.rm=T)
col3<-svymean(cces12_general$college[cces12_general$presgeneral_party=="Democrat" | cces12_general$pid7<4], design=cces12_general.design[cces12_general$presgeneral_party=="Democrat" | cces12_general$pid7<4], na.rm=T)

col4<-svymean(cces12_primary$college[cces12_primary$primaryturnout=="Republican"], design=cces12_primary.design[cces12_primary$primaryturnout=="Republican"], na.rm=T)
col5<-svymean(cces12_general_noprim$college[cces12_general_noprim$presgeneral_party=="Republican"], design=cces12_general_noprim.design[cces12_general_noprim$presgeneral_party=="Republican"], na.rm=T)
col6<-svymean(cces12_general$college[cces12_general$presgeneral_party=="Republican" | cces12_general$pid7>4], design=cces12_general.design[cces12_general$presgeneral_party=="Republican" | cces12_general$pid7>4], na.rm=T)

diff_a<-col1-col3
diff_b<-col4-col6
z_test_a = (col1-col3)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col3)$SE^2)
z_test_b= (col4-col6)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col6)$SE^2)

col1<-paste(round(col1*100,0), "%",sep="")
col2<-paste(round(col2*100,0), "%",sep="")
col3<-paste(round(col3*100,0), "%",sep="")
col4<-paste(round(col4*100,0), "%",sep="")
col5<-paste(round(col5*100,0), "%",sep="")
col6<-paste(round(col6*100,0), "%",sep="")
diff_a<-paste(round(diff_a*100,0), "%",sep="")
diff_b<-paste(round(diff_b*100,0), "%",sep="")

#xtable(as.matrix(cbind(col1, col3, diff_a,NA, col4, col6, diff_b)))
xtable(as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6))); mat <- as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6)); rownames(mat) <- "college degree"; fulltable <- rbind(fulltable,mat)

col4.mean<-svymean(cces12_primary$college[cces12_primary$primaryturnout=="Republican"], design=cces12_primary.design[cces12_primary$primaryturnout=="Republican"], na.rm=T)
col5.mean<-svymean(cces12_general_noprim$college[cces12_general_noprim$presgeneral_party=="Republican"], design=cces12_general_noprim.design[cces12_general_noprim$presgeneral_party=="Republican"], na.rm=T)
col6.mean<-svymean(cces12_general$college[cces12_general$presgeneral_party=="Republican" | cces12_general$pid7>4], design=cces12_general.design[cces12_general$presgeneral_party=="Republican" | cces12_general$pid7>4], na.rm=T)
xtable(as.matrix(cbind(col4.mean, col5.mean, col6.mean)))
xtable(as.matrix(cbind(
paste("[",round(as.data.frame(col4.mean)$mean-1.96*as.data.frame(col4.mean)$SE,2), ",",round(as.data.frame(col4.mean)$mean+1.96*as.data.frame(col4.mean)$SE,2),
"]", sep=""),
paste("[",round(as.data.frame(col5.mean)$mean-1.96*as.data.frame(col5.mean)$SE,2), ",",round(as.data.frame(col5.mean)$mean+1.96*as.data.frame(col5.mean)$SE,2),
"]", sep=""),
paste("[",round(as.data.frame(col6.mean)$mean-1.96*as.data.frame(col6.mean)$SE,2), ", ",round(as.data.frame(col6.mean)$mean+1.96*as.data.frame(col6.mean)$SE,2),
"]", sep=""))))

## white
col1<-svymean(cces12_primary$white[cces12_primary$primaryturnout=="Democrat"], design=cces12_primary.design[cces12_primary$primaryturnout=="Democrat"], na.rm=T)
col2<-svymean(cces12_general_noprim$white[cces12_general_noprim$presgeneral_party=="Democrat"], design=cces12_general_noprim.design[cces12_general_noprim$presgeneral_party=="Democrat"], na.rm=T)
col3<-svymean(cces12_general$white[cces12_general$presgeneral_party=="Democrat" | cces12_general$pid7<4], design=cces12_general.design[cces12_general$presgeneral_party=="Democrat" | cces12_general$pid7<4], na.rm=T)

col4<-svymean(cces12_primary$white[cces12_primary$primaryturnout=="Republican"], design=cces12_primary.design[cces12_primary$primaryturnout=="Republican"], na.rm=T)
col5<-svymean(cces12_general_noprim$white[cces12_general_noprim$presgeneral_party=="Republican"], design=cces12_general_noprim.design[cces12_general_noprim$presgeneral_party=="Republican"], na.rm=T)
col6<-svymean(cces12_general$white[cces12_general$presgeneral_party=="Republican" | cces12_general$pid7>4], design=cces12_general.design[cces12_general$presgeneral_party=="Republican" | cces12_general$pid7>4], na.rm=T)
diff_a<-col1-col3
diff_b<-col4-col6
z_test_a = (col1-col3)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col3)$SE^2)
z_test_b= (col4-col6)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col6)$SE^2)

col1<-paste(round(col1*100,0), "%",sep="")
col2<-paste(round(col2*100,0), "%",sep="")
col3<-paste(round(col3*100,0), "%",sep="")
col4<-paste(round(col4*100,0), "%",sep="")
col5<-paste(round(col5*100,0), "%",sep="")
col6<-paste(round(col6*100,0), "%",sep="")
diff_a<-paste(round(diff_a*100,0), "%",sep="")
diff_b<-paste(round(diff_b*100,0), "%",sep="")

#xtable(as.matrix(cbind(col1, col3, diff_a,NA, col4, col6, diff_b)))
xtable(as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6))); mat <- as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6)); rownames(mat) <- "white"; fulltable <- rbind(fulltable,mat)

col4.mean<-svymean(cces12_primary$white[cces12_primary$primaryturnout=="Republican"], design=cces12_primary.design[cces12_primary$primaryturnout=="Republican"], na.rm=T)
col5.mean<-svymean(cces12_general_noprim$white[cces12_general_noprim$presgeneral_party=="Republican"], design=cces12_general_noprim.design[cces12_general_noprim$presgeneral_party=="Republican"], na.rm=T)
col6.mean<-svymean(cces12_general$white[cces12_general$presgeneral_party=="Republican" | cces12_general$pid7>4], design=cces12_general.design[cces12_general$presgeneral_party=="Republican" | cces12_general$pid7>4], na.rm=T)
xtable(as.matrix(cbind(col4.mean, col5.mean, col6.mean)))
xtable(as.matrix(cbind(
paste("[",round(as.data.frame(col4.mean)$mean-1.96*as.data.frame(col4.mean)$SE,2), ",",round(as.data.frame(col4.mean)$mean+1.96*as.data.frame(col4.mean)$SE,2),
"]", sep=""),
paste("[",round(as.data.frame(col5.mean)$mean-1.96*as.data.frame(col5.mean)$SE,2), ",",round(as.data.frame(col5.mean)$mean+1.96*as.data.frame(col5.mean)$SE,2),
"]", sep=""),
paste("[",round(as.data.frame(col6.mean)$mean-1.96*as.data.frame(col6.mean)$SE,2), ", ",round(as.data.frame(col6.mean)$mean+1.96*as.data.frame(col6.mean)$SE,2),
"]", sep=""))))


## very interested in politics
col1<-svymean(cces12_primary$pol_interest[cces12_primary$primaryturnout=="Democrat"], design=cces12_primary.design[cces12_primary$primaryturnout=="Democrat"], na.rm=T)
col2<-svymean(cces12_general_noprim$pol_interest[cces12_general_noprim$presgeneral_party=="Democrat"], design=cces12_general_noprim.design[cces12_general_noprim$presgeneral_party=="Democrat"], na.rm=T)
col3<-svymean(cces12_general$pol_interest[cces12_general$presgeneral_party=="Democrat" | cces12_general$pid7<4], design=cces12_general.design[cces12_general$presgeneral_party=="Democrat" | cces12_general$pid7<4], na.rm=T)

col4<-svymean(cces12_primary$pol_interest[cces12_primary$primaryturnout=="Republican"], design=cces12_primary.design[cces12_primary$primaryturnout=="Republican"], na.rm=T)
col5<-svymean(cces12_general_noprim$pol_interest[cces12_general_noprim$presgeneral_party=="Republican"], design=cces12_general_noprim.design[cces12_general_noprim$presgeneral_party=="Republican"], na.rm=T)
col6<-svymean(cces12_general$pol_interest[cces12_general$presgeneral_party=="Republican" | cces12_general$pid7>4], design=cces12_general.design[cces12_general$presgeneral_party=="Republican" | cces12_general$pid7>4], na.rm=T)

diff_a<-col1-col3
diff_b<-col4-col6
z_test_a = (col1-col3)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col3)$SE^2)
z_test_b= (col4-col6)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col6)$SE^2)

col1<-paste(round(col1*100,0), "%",sep="")
col2<-paste(round(col2*100,0), "%",sep="")
col3<-paste(round(col3*100,0), "%",sep="")
col4<-paste(round(col4*100,0), "%",sep="")
col5<-paste(round(col5*100,0), "%",sep="")
col6<-paste(round(col6*100,0), "%",sep="")
diff_a<-paste(round(diff_a*100,0), "%",sep="")
diff_b<-paste(round(diff_b*100,0), "%",sep="")

#xtable(as.matrix(cbind(col1, col3, diff_a,NA, col4, col6, diff_b)))
xtable(as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6))); mat <- as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6)); rownames(mat) <- "very interested"; fulltable <- rbind(fulltable,mat)

col4.mean<-svymean(cces12_primary$pol_interest[cces12_primary$primaryturnout=="Republican"], design=cces12_primary.design[cces12_primary$primaryturnout=="Republican"], na.rm=T)
col5.mean<-svymean(cces12_general_noprim$pol_interest[cces12_general_noprim$presgeneral_party=="Republican"], design=cces12_general_noprim.design[cces12_general_noprim$presgeneral_party=="Republican"], na.rm=T)
col6.mean<-svymean(cces12_general$pol_interest[cces12_general$presgeneral_party=="Republican" | cces12_general$pid7>4], design=cces12_general.design[cces12_general$presgeneral_party=="Republican" | cces12_general$pid7>4], na.rm=T)
xtable(as.matrix(cbind(col4.mean, col5.mean, col6.mean)))
xtable(as.matrix(cbind(
paste("[",round(as.data.frame(col4.mean)$mean-1.96*as.data.frame(col4.mean)$SE,2), ",",round(as.data.frame(col4.mean)$mean+1.96*as.data.frame(col4.mean)$SE,2),
"]", sep=""),
paste("[",round(as.data.frame(col5.mean)$mean-1.96*as.data.frame(col5.mean)$SE,2), ",",round(as.data.frame(col5.mean)$mean+1.96*as.data.frame(col5.mean)$SE,2),
"]", sep=""),
paste("[",round(as.data.frame(col6.mean)$mean-1.96*as.data.frame(col6.mean)$SE,2), ", ",round(as.data.frame(col6.mean)$mean+1.96*as.data.frame(col6.mean)$SE,2),
"]", sep=""))))

## donated to a candidate
col1<-svymean(cces12_primary$donor[cces12_primary$primaryturnout=="Democrat"], design=cces12_primary.design[cces12_primary$primaryturnout=="Democrat"], na.rm=T)
col2<-svymean(cces12_general_noprim$donor[cces12_general_noprim$presgeneral_party=="Democrat"], design=cces12_general_noprim.design[cces12_general_noprim$presgeneral_party=="Democrat"], na.rm=T)
col3<-svymean(cces12_general$donor[cces12_general$presgeneral_party=="Democrat" | cces12_general$pid7<4], design=cces12_general.design[cces12_general$presgeneral_party=="Democrat" | cces12_general$pid7<4], na.rm=T)
diff<-col1-col2
z_test1 = (col1-col2)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col2)$SE^2)

col4<-svymean(cces12_primary$donor[cces12_primary$primaryturnout=="Republican"], design=cces12_primary.design[cces12_primary$primaryturnout=="Republican"], na.rm=T)
col5<-svymean(cces12_general_noprim$donor[cces12_general_noprim$presgeneral_party=="Republican"], design=cces12_general_noprim.design[cces12_general_noprim$presgeneral_party=="Republican"], na.rm=T)
col6<-svymean(cces12_general$donor[cces12_general$presgeneral_party=="Republican" | cces12_general$pid7>4], design=cces12_general.design[cces12_general$presgeneral_party=="Republican" | cces12_general$pid7>4], na.rm=T)
z_test2 = (col4-col5)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col5)$SE^2)

diff_a<-col1-col3
diff_b<-col4-col6
z_test_a = (col1-col3)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col3)$SE^2)
z_test_b= (col4-col6)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col6)$SE^2)

col1<-paste(round(col1*100,0), "%",sep="")
col2<-paste(round(col2*100,0), "%",sep="")
col3<-paste(round(col3*100,0), "%",sep="")
col4<-paste(round(col4*100,0), "%",sep="")
col5<-paste(round(col5*100,0), "%",sep="")
col6<-paste(round(col6*100,0), "%",sep="")
diff_a<-paste(round(diff_a*100,0), "%",sep="")
diff_b<-paste(round(diff_b*100,0), "%",sep="")

#xtable(as.matrix(cbind(col1, col3, diff_a,NA, col4, col6, diff_b)))
xtable(as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6))); mat <- as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6)); rownames(mat) <- "donated"; fulltable <- rbind(fulltable,mat)


## ideological self-placement
col1<-svymean(cces12_primary$self_ideology[cces12_primary$primaryturnout=="Democrat"], design=cces12_primary.design[cces12_primary$primaryturnout=="Democrat"], na.rm=T)
col2<-svymean(cces12_general_noprim$self_ideology[cces12_general_noprim$presgeneral_party=="Democrat"], design=cces12_general_noprim.design[cces12_general_noprim$presgeneral_party=="Democrat"], na.rm=T)
col3<-svymean(cces12_general$self_ideology[cces12_general$presgeneral_party=="Democrat" | cces12_general$pid7<4], design=cces12_general.design[cces12_general$presgeneral_party=="Democrat" | cces12_general$pid7<4], na.rm=T)

col4<-svymean(cces12_primary$self_ideology[cces12_primary$primaryturnout=="Republican"], design=cces12_primary.design[cces12_primary$primaryturnout=="Republican"], na.rm=T)
col5<-svymean(cces12_general_noprim$self_ideology[cces12_general_noprim$presgeneral_party=="Republican"], design=cces12_general_noprim.design[cces12_general_noprim$presgeneral_party=="Republican"], na.rm=T)
col6<-svymean(cces12_general$self_ideology[cces12_general$presgeneral_party=="Republican" | cces12_general$pid7>4], design=cces12_general.design[cces12_general$presgeneral_party=="Republican" | cces12_general$pid7>4], na.rm=T)

diff_a<-col1-col3
diff_b<-col4-col6
z_test_a = (col1-col3)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col3)$SE^2)
z_test_b= (col4-col6)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col6)$SE^2)

#xtable(as.matrix(cbind(col1, col3, diff_a,NA, col4, col6, diff_b)))
xtable(as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6))); mat <- as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6)); rownames(mat) <- "self-placement"; fulltable <- rbind(fulltable,mat)

col4.mean<-svymean(cces12_primary$self_ideology[cces12_primary$primaryturnout=="Republican"], design=cces12_primary.design[cces12_primary$primaryturnout=="Republican"], na.rm=T)
col5.mean<-svymean(cces12_general_noprim$self_ideology[cces12_general_noprim$presgeneral_party=="Republican"], design=cces12_general_noprim.design[cces12_general_noprim$presgeneral_party=="Republican"], na.rm=T)
col6.mean<-svymean(cces12_general$self_ideology[cces12_general$presgeneral_party=="Republican" | cces12_general$pid7>4], design=cces12_general.design[cces12_general$presgeneral_party=="Republican" | cces12_general$pid7>4], na.rm=T)
xtable(as.matrix(cbind(col4.mean, col5.mean, col6.mean)))
xtable(as.matrix(cbind(
paste("[",round(as.data.frame(col4.mean)$mean-1.96*as.data.frame(col4.mean)$SE,2), ",",round(as.data.frame(col4.mean)$mean+1.96*as.data.frame(col4.mean)$SE,2),
"]", sep=""),
paste("[",round(as.data.frame(col5.mean)$mean-1.96*as.data.frame(col5.mean)$SE,2), ",",round(as.data.frame(col5.mean)$mean+1.96*as.data.frame(col5.mean)$SE,2),
"]", sep=""),
paste("[",round(as.data.frame(col6.mean)$mean-1.96*as.data.frame(col6.mean)$SE,2), ", ",round(as.data.frame(col6.mean)$mean+1.96*as.data.frame(col6.mean)$SE,2),
"]", sep=""))))


## Issue ideology
##CCES
col1<-svymean(cces12_primary$idealpoint[cces12_primary$primaryturnout=="Democrat" & cces12_primary$source=="CCES_2012"], design=cces12_primary.design[cces12_primary$primaryturnout=="Democrat" & cces12_primary$source=="CCES_2012"], na.rm=T)
col2<-svymean(cces12_general_noprim$idealpoint[cces12_general_noprim$presgeneral_party=="Democrat" & cces12_general_noprim$source=="CCES_2012"], design=cces12_general_noprim.design[cces12_general_noprim$presgeneral_party=="Democrat" & cces12_general_noprim$source=="CCES_2012"], na.rm=T)
col3<-svymean(cces12_general$idealpoint[cces12_general$presgeneral_party=="Democrat" | cces12_general$pid7<4 & cces12_general$source=="CCES_2012"], design=cces12_general.design[cces12_general$presgeneral_party=="Democrat" | cces12_general$pid7<4 & cces12_general$source=="CCES_2012"], na.rm=T)

col4<-svymean(cces12_primary$idealpoint[cces12_primary$primaryturnout=="Republican" & cces12_primary$source=="CCES_2012"], design=cces12_primary.design[cces12_primary$primaryturnout=="Republican" & cces12_primary$source=="CCES_2012"], na.rm=T)
col5<-svymean(cces12_general_noprim$idealpoint[cces12_general_noprim$presgeneral_party=="Republican" & cces12_general_noprim$source=="CCES_2012"], design=cces12_general_noprim.design[cces12_general_noprim$presgeneral_party=="Republican" & cces12_general_noprim$source=="CCES_2012"], na.rm=T)
col6<-svymean(cces12_general$idealpoint[cces12_general$presgeneral_party=="Republican" | cces12_general$pid7>4 & cces12_general$source=="CCES_2012"], design=cces12_general.design[cces12_general$presgeneral_party=="Republican" | cces12_general$pid7>4 & cces12_general$source=="CCES_2012"], na.rm=T)

diff_a<-col1-col3
diff_b<-col4-col6
z_test_a = (col1-col3)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col3)$SE^2)
z_test_b= (col4-col6)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col6)$SE^2)

#xtable(as.matrix(cbind(col1, col3, diff_a,NA, col4, col6, diff_b)))
xtable(as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6))); mat <- as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6)); rownames(mat) <- "ideology"; fulltable <- rbind(fulltable,mat)

##########
## Issues
##########


# Abortion
col1<-svymean(cces12_primary$cces2006_abortion[cces12_primary$primaryturnout=="Democrat"], design=cces12_primary.design[cces12_primary$primaryturnout=="Democrat"], na.rm=T)
col2<-svymean(cces12_general_noprim$cces2006_abortion[cces12_general_noprim$presgeneral_party=="Democrat"], design=cces12_general_noprim.design[cces12_general_noprim$presgeneral_party=="Democrat"], na.rm=T)
col3<-svymean(cces12_general$cces2006_abortion[cces12_general$presgeneral_party=="Democrat" | cces12_general$pid7<4], design=cces12_general.design[cces12_general$presgeneral_party=="Democrat" | cces12_general$pid7<4], na.rm=T)

col4<-svymean(cces12_primary$cces2006_abortion[cces12_primary$primaryturnout=="Republican"], design=cces12_primary.design[cces12_primary$primaryturnout=="Republican"], na.rm=T)
col5<-svymean(cces12_general_noprim$cces2006_abortion[cces12_general_noprim$presgeneral_party=="Republican"], design=cces12_general_noprim.design[cces12_general_noprim$presgeneral_party=="Republican"], na.rm=T)
col6<-svymean(cces12_general$cces2006_abortion[cces12_general$presgeneral_party=="Republican" | cces12_general$pid7>4], design=cces12_general.design[cces12_general$presgeneral_party=="Republican" | cces12_general$pid7>4], na.rm=T)

diff_a<-col1-col3
diff_b<-col4-col6
z_test_a = (col1-col3)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col3)$SE^2)
z_test_b= (col4-col6)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col6)$SE^2)

col1<-paste(round(col1*100,0), "%",sep="")
col2<-paste(round(col2*100,0), "%",sep="")
col3<-paste(round(col3*100,0), "%",sep="")
col4<-paste(round(col4*100,0), "%",sep="")
col5<-paste(round(col5*100,0), "%",sep="")
col6<-paste(round(col6*100,0), "%",sep="")
diff_a<-paste(round(diff_a*100,0), "%",sep="")
diff_b<-paste(round(diff_b*100,0), "%",sep="")

#xtable(as.matrix(cbind(col1, col3, diff_a,NA, col4, col6, diff_b)))
xtable(as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6))); mat <- as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6)); rownames(mat) <- "abortion"; fulltable <- rbind(fulltable,mat)

# Health Reform 
col1<-svymean(cces12_primary$cces2012_supportaca[cces12_primary$primaryturnout=="Democrat"], design=cces12_primary.design[cces12_primary$primaryturnout=="Democrat"], na.rm=T)
col2<-svymean(cces12_general_noprim$cces2012_supportaca[cces12_general_noprim$presgeneral_party=="Democrat"], design=cces12_general_noprim.design[cces12_general_noprim$presgeneral_party=="Democrat"], na.rm=T)
col3<-svymean(cces12_general$cces2012_supportaca[cces12_general$presgeneral_party=="Democrat" | cces12_general$pid7<4], design=cces12_general.design[cces12_general$presgeneral_party=="Democrat" | cces12_general$pid7<4], na.rm=T)

col4<-svymean(cces12_primary$cces2012_supportaca[cces12_primary$primaryturnout=="Republican"], design=cces12_primary.design[cces12_primary$primaryturnout=="Republican"], na.rm=T)
col5<-svymean(cces12_general_noprim$cces2012_supportaca[cces12_general_noprim$presgeneral_party=="Republican"], design=cces12_general_noprim.design[cces12_general_noprim$presgeneral_party=="Republican"], na.rm=T)
col6<-svymean(cces12_general$cces2012_supportaca[cces12_general$presgeneral_party=="Republican" | cces12_general$pid7>4], design=cces12_general.design[cces12_general$presgeneral_party=="Republican" | cces12_general$pid7>4], na.rm=T)

diff_a<-col1-col3
diff_b<-col4-col6
z_test_a = (col1-col3)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col3)$SE^2)
z_test_b= (col4-col6)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col6)$SE^2)

col1<-paste(round(col1*100,0), "%",sep="")
col2<-paste(round(col2*100,0), "%",sep="")
col3<-paste(round(col3*100,0), "%",sep="")
col4<-paste(round(col4*100,0), "%",sep="")
col5<-paste(round(col5*100,0), "%",sep="")
col6<-paste(round(col6*100,0), "%",sep="")
diff_a<-paste(round(diff_a*100,0), "%",sep="")
diff_b<-paste(round(diff_b*100,0), "%",sep="")

#xtable(as.matrix(cbind(col1, col3, diff_a,NA, col4, col6, diff_b)))
xtable(as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6))); mat <- as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6)); rownames(mat) <- "health reform"; fulltable <- rbind(fulltable,mat)


fulltable
xtab <- print(xtable(fulltable))
cat(xtab,file="cces2012pres.tex")

